home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / src / matrix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-27  |  48.6 KB  |  2,398 lines

  1. /*
  2.  * $Id: draw.c,v 1.4 2001/02/01 14:36:49 tfrieden Exp $
  3.  *
  4.  * $Date: 2001/02/01 14:36:49 $
  5.  * $Revision: 1.4 $
  6.  *
  7.  * (C) 1999 by Hyperion
  8.  * All rights reserved
  9.  *
  10.  * This file is part of the MiniGL library project
  11.  * See the file Licence.txt for more details
  12.  *
  13.  */
  14.  
  15.  
  16. #include "sysinc.h"
  17. #include <math.h>
  18.  
  19. #include <stdlib.h>
  20. #ifdef __VBCC__
  21. #include <stdio.h>
  22. #endif
  23.  
  24. #ifndef PI
  25.     #ifdef M_PI
  26.     #define PI M_PI
  27.     #else
  28.     #define PI 3.1415927
  29.     #endif
  30. #endif
  31.  
  32. #ifdef DISABLE_TRANSFORMATION
  33. #warning "Compiling without transformation pipeline. Only flat geometry supported"
  34. #endif
  35.  
  36. static char rcsid[] = "$Id: draw.c,v 1.4 2001/02/01 14:36:49 tfrieden Exp $";
  37.  
  38. typedef void (*Multfn)(struct Matrix_t *, float *, struct Matrix_t *);
  39.  
  40. INLINE void m_MatCopy(Matrix *pA, Matrix *pB);
  41. INLINE void m_MatMultGeneral(Matrix *pA, float *pB, Matrix *pC);
  42.  
  43. INLINE void m_MatMultAIdent(float *pB, Matrix *pC); //surgeon
  44. INLINE void m_MatMultAPerspB0001(Matrix *pA, float *pB, Matrix *pC); //surgeon: common case for m_CombineMatrices
  45. INLINE void m_MatMultRotRot(Matrix *pA, float *pB, Matrix *pC); //surgeon
  46. INLINE void m_MatMultBRot001(Matrix *pA, float *pB, Matrix *pC); //surgeon
  47. INLINE void m_MatMultBRot010(Matrix *pA, float *pB, Matrix *pC); //surgeon
  48. INLINE void m_MatMultBRot100(Matrix *pA, float *pB, Matrix *pC); //surgeon
  49. INLINE void m_MatMultA0001_BRot001(Matrix *pA, float *pB, Matrix *pC); //surgeon
  50. INLINE void m_MatMultA0001_BRot010(Matrix *pA, float *pB, Matrix *pC); //surgeon
  51. INLINE void m_MatMultA0001_BRot100(Matrix *pA, float *pB, Matrix *pC); //surgeon
  52. INLINE void m_MatMultA0001_B0001(Matrix *pA, float *pB, Matrix *pC); //surgeon
  53.  
  54. INLINE void m_MatMultB0001(Matrix *pA, float *pB, Matrix *pC);
  55. INLINE void m_MatMultA0001(Matrix *pA, float *pB, Matrix *pC);
  56. INLINE void m_MatMultATrans(Matrix *pA, float *pB, Matrix *pC);
  57. INLINE void m_MatMultBTrans(Matrix *pA, float *pB, Matrix *pC);
  58. INLINE void m_MatMultBScale(Matrix *pA, float *pB, Matrix *pC); //surgeon
  59. INLINE void m_MatMultBOrtho(Matrix *pA, float *pB, Matrix *pC); //surgeon
  60. INLINE void m_MatMultBPersp(Matrix *pA, float *pB, Matrix *pC); //surgeon
  61.  
  62. INLINE void m_MatMultA0001_BTrans(Matrix *pA, float *pB, Matrix *pC); //surgeon
  63. INLINE void m_MatMultA0001_BScale(Matrix *pA, float *pB, Matrix *pC); //surgeon
  64. INLINE void m_MatMultA0001_BOrtho(Matrix *pA, float *pB, Matrix *pC); //surgeon
  65. INLINE void m_MatMultA0001_BPersp(Matrix *pA, float *pB, Matrix *pC); //surgeon
  66.  
  67. INLINE void m_LoadMatrixf(Matrix *pA, const float *v);
  68. INLINE void m_LoadMatrixd(Matrix *pA, const double *v);
  69. INLINE void m_LoadIdentity(Matrix *pA);
  70. INLINE void m_Mult(Matrix *pA, float *v, int vtype, Matrix *pC);
  71. void m_CombineMatrices(GLcontext context);
  72. void GLMatrixInit(GLcontext context);
  73.  
  74.  
  75. /* I am too lazy to convert our old code */
  76. #define CM_0 OF_11
  77. #define CM_1 OF_12
  78. #define CM_2 OF_13
  79. #define CM_3 OF_21
  80. #define CM_4 OF_22
  81. #define CM_5 OF_23
  82. #define CM_6 OF_31
  83. #define CM_7 OF_32
  84. #define CM_8 OF_33
  85.  
  86. /*
  87. ** Computes the determinant of the upper 3x3 submatrix block
  88. ** of the supplied matrix
  89. */
  90. INLINE GLfloat m_Determinant(Matrix *pA)
  91. {
  92.    #define a(x) (pA->v[CM_##x])
  93.    float a0 = a(0);
  94.    float a1 = a(1);
  95.    float a2 = a(2);
  96.    float a3 = a(3);
  97.    float a4 = a(4);
  98.    float a5 = a(5);
  99.    float a6 = a(6);
  100.    float a7 = a(7);
  101.    float a8 = a(8);
  102.  
  103.  
  104.     return ( a0*a4*a8  +  a1*a5*a6  + a2*a3*a7 -  a0*a5*a7  -  a1*a3*a8  - a2*a4*a6);
  105.  
  106.    #undef a
  107. }
  108.  
  109. /*
  110. ** Builds the inverse of the upper 3x3 submatrix block of the
  111. ** supplied matrix for the backtransformation of the normals
  112. */
  113.  
  114. void m_DoInvert(Matrix *pA, int flags, GLfloat *out)
  115. {
  116.  
  117.    #define a(x) (pA->v[CM_##x])
  118.  
  119.    float a0 = a(0);
  120.    float a1 = a(1);
  121.    float a2 = a(2);
  122.    float a3 = a(3);
  123.    float a4 = a(4);
  124.    float a5 = a(5);
  125.    float a6 = a(6);
  126.    float a7 = a(7);
  127.    float a8 = a(8);
  128.  
  129.    GLfloat det = a0*a4*a8 + a1*a5*a6 + a2*a3*a7
  130.          - a0*a5*a7 - a1*a3*a8 - a2*a4*a6;
  131.  
  132.    det = 1.0 / det;
  133.  
  134.    *out++ = (GLfloat)(a4*a8 - a5*a7)*det;
  135.    *out++ = (GLfloat)(a2*a7 - a1*a8)*det;
  136.    *out++ = (GLfloat)(a1*a5 - a2*a4)*det;
  137.  
  138.    *out++ = (GLfloat)(a5*a6 - a3*a8)*det;
  139.    *out++ = (GLfloat)(a0*a8 - a2*a6)*det;
  140.    *out++ = (GLfloat)(a2*a3 - a0*a5)*det;
  141.    
  142.    *out++ = (GLfloat)(a3*a7 - a4*a6)*det;
  143.    *out++ = (GLfloat)(a1*a6 - a0*a7)*det;
  144.    *out++ = (GLfloat)(a0*a4 - a1*a3)*det;
  145.  
  146.    #undef a
  147. }
  148.  
  149. void m_PrintMatrix(Matrix *pA);
  150.  
  151. void m_BuildInverted(GLcontext context)
  152. {
  153. //   m_PrintMatrix(CurrentMV);
  154. //   printf("flags: %i\n\n", CurrentMV->flags);
  155.  
  156.    context->InvRotValid = GL_TRUE;
  157.    m_DoInvert(CurrentMV, CurrentMV->flags, context->InvRot);
  158. }
  159.  
  160.  
  161. /*
  162. ** Copy matrix B to matrix A
  163. ** A = B
  164. */
  165. INLINE void m_MatCopy(Matrix *pA, Matrix *pB)
  166. {
  167.    int i;
  168.  
  169. //   for (i=0; i<16; i++)
  170.    i = 0;
  171.    do{
  172.  
  173.     pA->v[i] = pB->v[i];
  174.     i++;
  175.     } while (i<16);
  176.  
  177.    pA->flags = pB->flags;
  178.    pA->Inverse = pB->Inverse;
  179. }
  180.  
  181. /*
  182. ** General case: Matrix multiply
  183. ** Multiply matrix A by float field, storing the result in matrix C
  184. */
  185. INLINE void m_MatMultGeneral(Matrix *pA, float *pB, Matrix *pC)
  186. {
  187.    #define a(x) (pA->v[OF_##x])
  188.    #define b(x) (pB[OF_##x])
  189.    #define c(x) (pC->v[OF_##x])
  190.  
  191.    float b11 = b(11);
  192.    float b12 = b(12);
  193.    float b13 = b(13);
  194.    float b14 = b(14);
  195.    float b21 = b(21);
  196.    float b22 = b(22);
  197.    float b23 = b(23);
  198.    float b24 = b(24);
  199.    float b31 = b(31);
  200.    float b32 = b(32);
  201.    float b33 = b(33);
  202.    float b34 = b(34);
  203.    float b41 = b(41);
  204.    float b42 = b(42);
  205.    float b43 = b(43);
  206.    float b44 = b(44);
  207.  
  208. float a11 = a(11);
  209. float a12 = a(12);
  210. float a13 = a(13);
  211. float a14 = a(14);
  212.  
  213. float a21 = a(21);
  214. float a22 = a(22);
  215. float a23 = a(23);
  216. float a24 = a(24);
  217.  
  218. float a31 = a(31);
  219. float a32 = a(32);
  220. float a33 = a(33);
  221. float a34 = a(34);
  222.  
  223. float a41 = a(41);
  224. float a42 = a(42);
  225. float a43 = a(43);
  226. float a44 = a(44);
  227.  
  228.    c(11)  = a11*b11;
  229.    c(12)  = a11*b12;
  230.    c(13)  = a11*b13;
  231.    c(11) += a12*b21;
  232.    c(12) += a12*b22;
  233.    c(13) += a12*b23;
  234.    c(11) += a13*b31;
  235.    c(12) += a13*b32;
  236.    c(13) += a13*b33;
  237.    c(11) += a14*b41;
  238.    c(12) += a14*b42;
  239.    c(13) += a14*b43;
  240.  
  241.    c(21)  = a21*b11;
  242.    c(22)  = a21*b12;
  243.    c(23)  = a21*b13;
  244.    c(21) += a22*b21;
  245.    c(22) += a22*b22;
  246.    c(23) += a22*b23;
  247.    c(21) += a23*b31;
  248.    c(22) += a23*b32;
  249.    c(23) += a23*b33;
  250.    c(21) += a24*b41;
  251.    c(22) += a24*b42;
  252.    c(23) += a24*b43;
  253.  
  254.    c(31)  = a31*b11;
  255.    c(32)  = a31*b12;
  256.    c(33)  = a31*b13;
  257.    c(31) += a32*b21;
  258.    c(32) += a32*b22;
  259.    c(33) += a32*b23;
  260.    c(31) += a33*b31;
  261.    c(32) += a33*b32;
  262.    c(33) += a33*b33;
  263.    c(31) += a34*b41;
  264.    c(32) += a34*b42;
  265.    c(33) += a34*b43;
  266.  
  267.    c(41)  = a41*b11;
  268.    c(42)  = a41*b12;
  269.    c(43)  = a41*b13;
  270.    c(41) += a42*b21;
  271.    c(42) += a42*b22;
  272.    c(43) += a42*b23;
  273.    c(41) += a43*b31;
  274.    c(42) += a43*b32;
  275.    c(43) += a43*b33;
  276.    c(41) += a44*b41; 
  277.    c(42) += a44*b42;
  278.    c(43) += a44*b43;
  279.  
  280.    c(14)  = b14*a11;
  281.    c(24)  = b14*a21;
  282.    c(34)  = b14*a31;
  283.    c(44)  = b14*a41;
  284.  
  285.    c(14) += b24*a12;
  286.    c(24) += b24*a22;
  287.    c(34) += b24*a32;
  288.    c(44) += b24*a42;
  289.  
  290.    c(14) += b34*a13;
  291.    c(24) += b34*a23;
  292.    c(34) += b34*a33;
  293.    c(44) += b34*a43;
  294.  
  295.    c(14) += b44*a14;
  296.    c(24) += b44*a24;
  297.    c(34) += b44*a34;
  298.    c(44) += b44*a44;
  299.  
  300.    #undef a
  301.    #undef b
  302. }
  303.  
  304.  
  305.  
  306. /*
  307. ** Matrix A is an identity matrix
  308. ** Multiply matrix A by float field, storing the result in matrix C
  309. */
  310. INLINE void m_MatMultAIdent(float *pB, Matrix *pC)
  311. {
  312.    int i;
  313.  
  314. //for (i=0; i<16; i++)
  315.    i = 0;
  316.     do {
  317.         pC->v[i] = pB[i];
  318.         i++;
  319.     } while(i < 16);
  320. }
  321.  
  322. /*
  323. ** Matrix A and matrix B are rotation matrices
  324. ** Multiply matrix A by float field, storing the result in matrix C
  325. */
  326. INLINE void m_MatMultRotRot(Matrix *pA, float *pB, Matrix *pC)
  327. {
  328.    #define a(x) (pA->v[OF_##x])
  329.    #define b(x) (pB[OF_##x])
  330.    #define c(x) (pC->v[OF_##x])
  331.  
  332.    float a11 = a(11),  b11 = b(11);
  333.    float a12 = a(12),  b12 = b(12);
  334.    float a13 = a(13),  b13 = b(13);
  335.    float a21 = a(21),  b21 = b(21);
  336.    float a22 = a(22),  b22 = b(22);
  337.    float a23 = a(23),  b23 = b(23);
  338.    float a31 = a(31),  b31 = b(31);
  339.    float a32 = a(32),  b32 = b(32);
  340.    float a33 = a(33),  b33 = b(33);
  341.  
  342.    c(11) = a11*b11 + a12*b21 + a13*b31;
  343.    c(12) = a11*b12 + a12*b22 + a13*b32;
  344.    c(13) = a11*b13 + a12*b23 + a13*b33;
  345.    c(14) = 0.f;
  346.  
  347.  
  348.    c(21) = a21*b11 + a22*b21 + a23*b31;
  349.    c(22) = a21*b12 + a22*b22 + a23*b32;
  350.    c(23) = a21*b13 + a22*b23 + a23*b33;
  351.    c(24) = 0.f;
  352.  
  353.    c(31) = a31*b11 + a32*b21 + a33*b31;
  354.    c(32) = a31*b12 + a32*b22 + a33*b32;
  355.    c(33) = a31*b13 + a32*b23 + a33*b33;
  356.    c(34) = 0.f;
  357.  
  358.    c(41) = 0.f;
  359.    c(42) = 0.f;
  360.    c(43) = 0.f;
  361.    c(44) = 1.f;
  362.  
  363.    #undef a
  364.    #undef b
  365. }
  366.  
  367. /*
  368. ** Matrix A is a perspective matrix
  369. ** Matrix B has three rows
  370. ** Multiply matrix A by float field, storing the result in matrix C
  371. */
  372. INLINE void m_MatMultAPerspB0001(Matrix *pA, float *pB, Matrix *pC)
  373. {
  374.    #define a(x) (pA->v[OF_##x])
  375.    #define b(x) (pB[OF_##x])
  376.    #define c(x) (pC->v[OF_##x])
  377.  
  378.    float a11 = a(11),  b11 = b(11);
  379.    float          b12 = b(12);
  380.    float a13 = a(13),  b13 = b(13);
  381.    float          b14 = b(14);
  382.    float          b21 = b(21);
  383.    float a22 = a(22),  b22 = b(22);
  384.    float a23 = a(23),  b23 = b(23);
  385.    float          b24 = b(24);
  386.    float          b31 = b(31);
  387.    float          b32 = b(32);
  388.    float a33 = a(33),  b33 = b(33);
  389.    float a34 = a(34),  b34 = b(34);
  390.  
  391.    c(11) = a11*b11 + a13*b31;
  392.    c(12) = a11*b12 + a13*b32;
  393.    c(13) = a11*b13 + a13*b33;
  394.    c(14) = a11*b14 + a13*b34;
  395.  
  396.    c(21) = a22*b21 + a23*b31;
  397.    c(22) = a22*b22 + a23*b32;
  398.    c(23) = a22*b23 + a23*b33;
  399.    c(24) = a22*b24 + a23*b34;
  400.  
  401.    c(31) = a33*b31;
  402.    c(32) = a33*b32;
  403.    c(33) = a33*b33;
  404.    c(34) = a33*b34 + a34;
  405.  
  406.    c(41) = -b31;
  407.    c(42) = -b32;
  408.    c(43) = -b33;
  409.    c(44) = -b34;
  410.  
  411.    #undef a
  412.    #undef b
  413. }
  414.  
  415. /*
  416. ** Matrix B has three rows
  417. ** Multiply matrix A by float field, storing the result in matrix C
  418. */
  419. INLINE void m_MatMultB0001(Matrix *pA, float *pB, Matrix *pC)
  420. {
  421.    #define a(x) (pA->v[OF_##x])
  422.    #define b(x) (pB[OF_##x])
  423.    #define c(x) (pC->v[OF_##x])
  424.  
  425.    float a11 = a(11),  b11 = b(11);
  426.    float a12 = a(12),  b12 = b(12);
  427.    float a13 = a(13),  b13 = b(13);
  428.    float a14 = a(14),  b14 = b(14);
  429.    float a21 = a(21),  b21 = b(21);
  430.    float a22 = a(22),  b22 = b(22);
  431.    float a23 = a(23),  b23 = b(23);
  432.    float a24 = a(24),  b24 = b(24);
  433.    float a31 = a(31),  b31 = b(31);
  434.    float a32 = a(32),  b32 = b(32);
  435.    float a33 = a(33),  b33 = b(33);
  436.    float a34 = a(34),  b34 = b(34);
  437.    float a41 = a(41);
  438.    float a42 = a(42);
  439.    float a43 = a(43);
  440.    float a44 = a(44);
  441.  
  442.  
  443.    c(11) = a11*b11 + a12*b21 + a13*b31;
  444.    c(12) = a11*b12 + a12*b22 + a13*b32;
  445.    c(13) = a11*b13 + a12*b23 + a13*b33;
  446.  
  447.    c(21) = a21*b11 + a22*b21 + a23*b31;
  448.    c(22) = a21*b12 + a22*b22 + a23*b32;
  449.    c(23) = a21*b13 + a22*b23 + a23*b33;
  450.  
  451.    c(31) = a31*b11 + a32*b21 + a33*b31;
  452.    c(32) = a31*b12 + a32*b22 + a33*b32;
  453.    c(33) = a31*b13 + a32*b23 + a33*b33;
  454.  
  455.    c(41) = a41*b11 + a42*b21 + a43*b31;
  456.    c(42) = a41*b12 + a42*b22 + a43*b32;
  457.    c(43) = a41*b13 + a42*b23 + a43*b33;
  458.  
  459. a14 += b34*a13;
  460. a24 += b34*a23;
  461. a34 += b34*a33;
  462. a44 += b34*a43;
  463.  
  464. a14 += b24*a12;
  465. a24 += b24*a22;
  466. a34 += b24*a32;
  467. a44 += b24*a42;
  468.  
  469. a14 += b14*a11;
  470. a24 += b14*a21;
  471. a34 += b14*a31;
  472. a44 += b14*a41;
  473.  
  474. c(14) = a14; 
  475. c(24) = a24; 
  476. c(34) = a34; 
  477. c(44) = a44; 
  478.  
  479.    #undef a
  480.    #undef b
  481. }
  482.  
  483. // --> surgeon begin
  484.  
  485. /*
  486. ** Matrix B has three rows
  487. ** Matrix A has three rows
  488. ** Multiply matrix A by float field, storing the result in matrix C
  489. */
  490. INLINE void m_MatMultA0001_B0001(Matrix *pA, float *pB, Matrix *pC)
  491. {
  492.    #define a(x) (pA->v[OF_##x])
  493.    #define b(x) (pB[OF_##x])
  494.    #define c(x) (pC->v[OF_##x])
  495.  
  496.    float a11 = a(11),  b11 = b(11);
  497.    float a12 = a(12),  b12 = b(12);
  498.    float a13 = a(13),  b13 = b(13);
  499.    float a14 = a(14),  b14 = b(14);
  500.    float a21 = a(21),  b21 = b(21);
  501.    float a22 = a(22),  b22 = b(22);
  502.    float a23 = a(23),  b23 = b(23);
  503.    float a24 = a(24),  b24 = b(24);
  504.    float a31 = a(31),  b31 = b(31);
  505.    float a32 = a(32),  b32 = b(32);
  506.    float a33 = a(33),  b33 = b(33);
  507.    float a34 = a(34),  b34 = b(34);
  508.  
  509.    c(11) = a11*b11 + a12*b21 + a13*b31;
  510.    c(12) = a11*b12 + a12*b22 + a13*b32;
  511.    c(13) = a11*b13 + a12*b23 + a13*b33;
  512.  
  513.    c(21) = a21*b11 + a22*b21 + a23*b31;
  514.    c(22) = a21*b12 + a22*b22 + a23*b32;
  515.    c(23) = a21*b13 + a22*b23 + a23*b33;
  516.  
  517.    c(31) = a31*b11 + a32*b21 + a33*b31;
  518.    c(32) = a31*b12 + a32*b22 + a33*b32;
  519.    c(33) = a31*b13 + a32*b23 + a33*b33;
  520.  
  521.    c(41) = 0.f;
  522.    c(42) = 0.f;
  523.    c(43) = 0.f;
  524.    c(44) = 1.f;
  525.  
  526. a14 += b34*a13;
  527. a24 += b34*a23;
  528. a34 += b34*a33;
  529.  
  530. a14 += b24*a12;
  531. a24 += b24*a22;
  532. a34 += b24*a32;
  533.  
  534. a14 += b14*a11;
  535. a24 += b14*a21;
  536. a34 += b14*a31;
  537.  
  538. c(14) = a14; 
  539. c(24) = a24; 
  540. c(34) = a34; 
  541.  
  542.    #undef a
  543.    #undef b
  544. }
  545.  
  546. /*
  547. ** Matrix B has three rows
  548. ** Matrix A has three rows
  549. ** Multiply matrix A by float field, storing the result in matrix C
  550. */
  551. INLINE void m_MatMultA0001_BRot001(Matrix *pA, float *pB, Matrix *pC)
  552. {
  553.    #define a(x) (pA->v[OF_##x])
  554.    #define b(x) (pB[OF_##x])
  555.    #define c(x) (pC->v[OF_##x])
  556.  
  557.    float a11 = a(11),  b11 = b(11);
  558.    float a12 = a(12),  b12 = b(12);
  559.    float a13 = a(13);
  560.    float a14 = a(14);
  561.    float a21 = a(21),  b21 = b(21);
  562.    float a22 = a(22),  b22 = b(22);
  563.    float a23 = a(23);
  564.    float a24 = a(24);
  565.    float a31 = a(31);
  566.    float a32 = a(32);
  567.    float a33 = a(33);
  568.    float a34 = a(34);
  569.  
  570.    c(11) = a11*b11 + a12*b21;
  571.    c(12) = a11*b12 + a12*b22;
  572.    c(13) = a13;
  573.    c(14) = a14;
  574.  
  575.    c(21) = a21*b11 + a22*b21;
  576.    c(22) = a21*b12 + a22*b22;
  577.    c(23) = a23;
  578.    c(24) = a24;
  579.  
  580.    c(31) = a31*b11 + a32*b21;
  581.    c(32) = a31*b12 + a32*b22;
  582.    c(33) = a33;
  583.    c(34) = a34;
  584.  
  585.    c(41) = 0.f;
  586.    c(42) = 0.f;
  587.    c(43) = 0.f;
  588.    c(44) = 1.f;
  589.  
  590.    #undef a
  591.    #undef b
  592. }
  593.  
  594. /*
  595. ** Matrix B has three rows
  596. ** Matrix A has three rows
  597. ** Multiply matrix A by float field, storing the result in matrix C
  598. */
  599. INLINE void m_MatMultA0001_BRot010(Matrix *pA, float *pB, Matrix *pC)
  600. {
  601.    #define a(x) (pA->v[OF_##x])
  602.    #define b(x) (pB[OF_##x])
  603.    #define c(x) (pC->v[OF_##x])
  604.  
  605.    float a11 = a(11),  b11 = b(11);
  606.    float a12 = a(12);
  607.    float a13 = a(13),  b13 = b(13);
  608.    float a14 = a(14);
  609.    float a21 = a(21);
  610.    float a22 = a(22);
  611.    float a23 = a(23);
  612.    float a24 = a(24);
  613.    float a31 = a(31),  b31 = b(31);
  614.    float a32 = a(32);
  615.    float a33 = a(33),  b33 = b(33);
  616.    float a34 = a(34);
  617.  
  618.    c(11) = a11*b11 + a13*b31;
  619.    c(12) = a12;
  620.    c(13) = a11*b13 + a13*b33;
  621.    c(14) = a14;
  622.  
  623.    c(21) = a21*b11 + a23*b31;
  624.    c(22) = a22;
  625.    c(23) = a21*b13 + a23*b33;
  626.    c(24) = a24;
  627.  
  628.    c(31) = a31*b11 + a33*b31;
  629.    c(32) = a32;
  630.    c(33) = a31*b13 + a33*b33;
  631.    c(34) = a34;
  632.  
  633.    c(41) = 0.f;
  634.    c(42) = 0.f;
  635.    c(43) = 0.f;
  636.    c(44) = 1.f;
  637.  
  638.    #undef a
  639.    #undef b
  640. }
  641.  
  642. /*
  643. ** Matrix B has three rows
  644. ** Matrix A has three rows
  645. ** Multiply matrix A by float field, storing the result in matrix C
  646. */
  647. INLINE void m_MatMultA0001_BRot100(Matrix *pA, float *pB, Matrix *pC)
  648. {
  649.    #define a(x) (pA->v[OF_##x])
  650.    #define b(x) (pB[OF_##x])
  651.    #define c(x) (pC->v[OF_##x])
  652.  
  653.    float a11 = a(11);
  654.    float a12 = a(12);
  655.    float a13 = a(13);
  656.    float a14 = a(14);
  657.    float a21 = a(21);
  658.    float a22 = a(22),  b22 = b(22);
  659.    float a23 = a(23),  b23 = b(23);
  660.    float a24 = a(24);
  661.    float a31 = a(31);
  662.    float a32 = a(32),  b32 = b(32);
  663.    float a33 = a(33),  b33 = b(33);
  664.    float a34 = a(34);
  665.  
  666.  
  667.    c(11) = a11;
  668.    c(12) = a12*b22 + a13*b32;
  669.    c(13) = a12*b23 + a13*b33;
  670.    c(14) = a14;
  671.  
  672.    c(21) = a21;
  673.    c(22) = a22*b22 + a23*b32;
  674.    c(23) = a22*b23 + a23*b33;
  675.    c(24) = a24;
  676.  
  677.    c(31) = a31;
  678.    c(32) = a32*b22 + a33*b32;
  679.    c(33) = a32*b23 + a33*b33;
  680.    c(34) = a34;
  681.  
  682.    c(41) = 0.f;
  683.    c(42) = 0.f;
  684.    c(43) = 0.f;
  685.    c(44) = 1.f;
  686.  
  687.    #undef a
  688.    #undef b
  689. }
  690.  
  691. /*
  692. ** Matrix B has three rows
  693. ** Multiply matrix A by float field, storing the result in matrix C
  694. */
  695. INLINE void m_MatMultBRot001(Matrix *pA, float *pB, Matrix *pC)
  696. {
  697.    #define a(x) (pA->v[OF_##x])
  698.    #define b(x) (pB[OF_##x])
  699.    #define c(x) (pC->v[OF_##x])
  700.  
  701.    float a11 = a(11),  b11 = b(11);
  702.    float a12 = a(12),  b12 = b(12);
  703.    float a13 = a(13);
  704.    float a14 = a(14);
  705.    float a21 = a(21),  b21 = b(21);
  706.    float a22 = a(22),  b22 = b(22);
  707.    float a23 = a(23);
  708.    float a24 = a(24);
  709.    float a31 = a(31);
  710.    float a32 = a(32);
  711.    float a33 = a(33);
  712.    float a34 = a(34);
  713.    float a41 = a(41);
  714.    float a42 = a(42);
  715.    float a43 = a(43);
  716.    float a44 = a(44);
  717.  
  718.    c(11) = a11*b11 + a12*b21;
  719.    c(12) = a11*b12 + a12*b22;
  720.    c(13) = a13;
  721.    c(14) = a14;
  722.  
  723.    c(21) = a21*b11 + a22*b21;
  724.    c(22) = a21*b12 + a22*b22;
  725.    c(23) = a23;
  726.    c(24) = a24;
  727.  
  728.    c(31) = a31*b11 + a32*b21;
  729.    c(32) = a31*b12 + a32*b22;
  730.    c(33) = a33;
  731.    c(34) = a34;
  732.  
  733.    c(41) = a41*b11 + a42*b21;
  734.    c(42) = a41*b12 + a42*b22;
  735.    c(43) = a43;
  736.    c(44) = a44;
  737.  
  738.    #undef a
  739.    #undef b
  740. }
  741.  
  742. /*
  743. ** Matrix B has three rows
  744. ** Multiply matrix A by float field, storing the result in matrix C
  745. */
  746. INLINE void m_MatMultBRot010(Matrix *pA, float *pB, Matrix *pC)
  747. {
  748.    #define a(x) (pA->v[OF_##x])
  749.    #define b(x) (pB[OF_##x])
  750.    #define c(x) (pC->v[OF_##x])
  751.  
  752.    float a11 = a(11),  b11 = b(11);
  753.    float a12 = a(12);
  754.    float a13 = a(13),  b13 = b(13);
  755.    float a14 = a(14);
  756.    float a21 = a(21);
  757.    float a22 = a(22);
  758.    float a23 = a(23);
  759.    float a24 = a(24);
  760.    float a31 = a(31),  b31 = b(31);
  761.    float a32 = a(32);
  762.    float a33 = a(33),  b33 = b(33);
  763.    float a34 = a(34);
  764.    float a41 = a(41);
  765.    float a42 = a(42);
  766.    float a43 = a(43);
  767.    float a44 = a(44);
  768.  
  769.    c(11) = a11*b11 + a13*b31;
  770.    c(12) = a12;
  771.    c(13) = a11*b13 + a13*b33;
  772.    c(14) = a14;
  773.  
  774.    c(21) = a21*b11 + a23*b31;
  775.    c(22) = a22;
  776.    c(23) = a21*b13 + a23*b33;
  777.    c(24) = a24;
  778.  
  779.    c(31) = a31*b11 + a33*b31;
  780.    c(32) = a32;
  781.    c(33) = a31*b13 + a33*b33;
  782.    c(34) = a34;
  783.  
  784.    c(41) = a41*b11 + a43*b31;
  785.    c(42) = a42;
  786.    c(43) = a41*b13 + a43*b33;
  787.    c(44) = a44;
  788.  
  789.    #undef a
  790.    #undef b
  791. }
  792.  
  793. /*
  794. ** Matrix B has three rows
  795. ** Multiply matrix A by float field, storing the result in matrix C
  796. */
  797. INLINE void m_MatMultBRot100(Matrix *pA, float *pB, Matrix *pC)
  798. {
  799.    #define a(x) (pA->v[OF_##x])
  800.    #define b(x) (pB[OF_##x])
  801.    #define c(x) (pC->v[OF_##x])
  802.  
  803.    float a11 = a(11);
  804.    float a12 = a(12);
  805.    float a13 = a(13);
  806.    float a14 = a(14);
  807.    float a21 = a(21);
  808.    float a22 = a(22),  b22 = b(22);
  809.    float a23 = a(23),  b23 = b(23);
  810.    float a24 = a(24);
  811.    float a31 = a(31);
  812.    float a32 = a(32),  b32 = b(32);
  813.    float a33 = a(33),  b33 = b(33);
  814.    float a34 = a(34);
  815.    float a41 = a(41);
  816.    float a42 = a(42);
  817.    float a43 = a(43);
  818.    float a44 = a(44);
  819.  
  820.  
  821.    c(11) = a11;
  822.    c(12) = a12*b22 + a13*b32;
  823.    c(13) = a12*b23 + a13*b33;
  824.    c(14) = a14;
  825.  
  826.    c(21) = a21;
  827.    c(22) = a22*b22 + a23*b32;
  828.    c(23) = a22*b23 + a23*b33;
  829.    c(24) = a24;
  830.  
  831.    c(31) = a31;
  832.    c(32) = a32*b22 + a33*b32;
  833.    c(33) = a32*b23 + a33*b33;
  834.    c(34) = a34;
  835.  
  836.    c(41) = a41;
  837.    c(42) = a42*b22 + a43*b32;
  838.    c(43) = a42*b23 + a43*b33;
  839.    c(44) = a44;
  840.  
  841.    #undef a
  842.    #undef b
  843. }
  844. // <--surgeon end
  845.  
  846. /*
  847. ** Matrix A has three rows
  848. ** Multiply matrix A by float field, storing the result in matrix C
  849. */
  850. INLINE void m_MatMultA0001(Matrix *pA, float *pB, Matrix *pC)
  851. {
  852.    #define a(x) (pA->v[OF_##x])
  853.    #define b(x) (pB[OF_##x])
  854.    #define c(x) (pC->v[OF_##x])
  855.  
  856.    float a11 = a(11),  b11 = b(11);
  857.    float a12 = a(12),  b12 = b(12);
  858.    float a13 = a(13),  b13 = b(13);
  859.    float a14 = a(14),  b14 = b(14);
  860.    float a21 = a(21),  b21 = b(21);
  861.    float a22 = a(22),  b22 = b(22);
  862.    float a23 = a(23),  b23 = b(23);
  863.    float a24 = a(24),  b24 = b(24);
  864.    float a31 = a(31),  b31 = b(31);
  865.    float a32 = a(32),  b32 = b(32);
  866.    float a33 = a(33),  b33 = b(33);
  867.    float a34 = a(34),  b34 = b(34);
  868.    float               b41 = b(41);
  869.    float               b42 = b(42);
  870.    float               b43 = b(43);
  871.    float               b44 = b(44);
  872.  
  873.  
  874.    c(11) = a11*b11 + a12*b21 + a13*b31 + a14*b41;
  875.    c(12) = a11*b12 + a12*b22 + a13*b32 + a14*b42;
  876.    c(13) = a11*b13 + a12*b23 + a13*b33 + a14*b43;
  877.    c(14) = a11*b14 + a12*b24 + a13*b34 + a14*b44;
  878.  
  879.    c(21) = a21*b11 + a22*b21 + a23*b31 + a24*b41;
  880.    c(22) = a21*b12 + a22*b22 + a23*b32 + a24*b42;
  881.    c(23) = a21*b13 + a22*b23 + a23*b33 + a24*b43;
  882.    c(24) = a21*b14 + a22*b24 + a23*b34 + a24*b44;
  883.  
  884.    c(31) = a31*b11 + a32*b21 + a33*b31 + a34*b41;
  885.    c(32) = a31*b12 + a32*b22 + a33*b32 + a34*b42;
  886.    c(33) = a31*b13 + a32*b23 + a33*b33 + a34*b43;
  887.    c(34) = a31*b14 + a32*b24 + a33*b34 + a34*b44;
  888.  
  889.    c(41) = b41;
  890.    c(42) = b42;
  891.    c(43) = b43;
  892.    c(44) = b44;
  893.  
  894.    #undef a
  895.    #undef b
  896. }
  897.  
  898. /*
  899. ** Matrix A is a translation matrix
  900. ** Multiply matrix A by float field, storing the result in matrix C
  901. */
  902. INLINE void m_MatMultATrans(Matrix *pA, float *pB, Matrix *pC)
  903. {
  904.    #define a(x) (pA->v[OF_##x])
  905.    #define b(x) (pB[OF_##x])
  906.    #define c(x) (pC->v[OF_##x])
  907.  
  908.    float               b11 = b(11);
  909.    float               b12 = b(12);
  910.    float               b13 = b(13);
  911.    float a14 = a(14),  b14 = b(14);
  912.    float               b21 = b(21);
  913.    float               b22 = b(22);
  914.    float               b23 = b(23);
  915.    float a24 = a(24),  b24 = b(24);
  916.    float               b31 = b(31);
  917.    float               b32 = b(32);
  918.    float               b33 = b(33);
  919.    float a34 = a(34),  b34 = b(34);
  920.    float               b41 = b(41);
  921.    float               b42 = b(42);
  922.    float               b43 = b(43);
  923.    float               b44 = b(44);
  924.  
  925.  
  926. b11 += a14*b41;
  927. b12 += a14*b42;
  928. b13 += a14*b43;
  929.    c(11) = b11;
  930.    c(12) = b12;
  931.    c(13) = b13;
  932.  
  933.  
  934. b21 += a24*b41;
  935. b22 += a24*b42;
  936. b23 += a24*b43;
  937.    c(21) = b21;
  938.    c(22) = b22;
  939.    c(23) = b23;
  940.  
  941.  
  942. b31 += a34*b41;
  943. b32 += a34*b42;
  944. b33 += a34*b43;
  945.    c(31) = b31;
  946.    c(32) = b32;
  947.    c(33) = b33;
  948.  
  949. b14 += b44*a14;
  950. b24 += b44*a24;
  951. b34 += b44*a34;
  952.    c(14) = b14;
  953.    c(24) = b23;
  954.    c(34) = b34;
  955.  
  956.    c(41) = b41;
  957.    c(42) = b42;
  958.    c(44) = b44;
  959.    c(43) = b43;
  960.  
  961.  
  962.  
  963.    #undef a
  964.    #undef b
  965. }
  966.  
  967. /*
  968. ** Matrix B is a translation matrix
  969. ** Multiply matrix A by float field, storing the result in matrix C
  970. */
  971. INLINE void m_MatMultBTrans(Matrix *pA, float *pB, Matrix *pC)
  972. {
  973.    #define a(x) (pA->v[OF_##x])
  974.    #define b(x) (pB[OF_##x])
  975.    #define c(x) (pC->v[OF_##x])
  976.  
  977.    float a11 = a(11);
  978.    float a12 = a(12);
  979.    float a13 = a(13);
  980.    float a14 = a(14),  b14 = b(14);
  981.    float a21 = a(21);
  982.    float a22 = a(22);
  983.    float a23 = a(23);
  984.    float a24 = a(24),  b24 = b(24);
  985.    float a31 = a(31);
  986.    float a32 = a(32);
  987.    float a33 = a(33);
  988.    float a34 = a(34),  b34 = b(34);
  989.    float a41 = a(41);
  990.    float a42 = a(42);
  991.    float a43 = a(43);
  992.    float a44 = a(44);
  993.  
  994.  
  995. a14 += b34*a13;
  996. a24 += b34*a23;
  997. a34 += b34*a33;
  998. a44 += b34*a43;
  999.  
  1000. a14 += b24*a12;
  1001. a24 += b24*a22;
  1002. a34 += b24*a32;
  1003. a44 += b24*a42;
  1004.  
  1005. a14 += b14*a11;
  1006. a24 += b14*a21;
  1007. a34 += b14*a31;
  1008. a44 += b14*a41;
  1009.  
  1010. c(14) = a14; 
  1011. c(24) = a24; 
  1012. c(34) = a34; 
  1013. c(44) = a44; 
  1014.  
  1015.    c(11) = a11;
  1016.    c(12) = a12;
  1017.    c(13) = a13;
  1018.  
  1019.    c(21) = a21;
  1020.    c(22) = a22;
  1021.    c(23) = a23;
  1022.  
  1023.    c(31) = a31;
  1024.    c(32) = a32;
  1025.    c(33) = a33;
  1026.  
  1027.    c(41) = a41;
  1028.    c(42) = a42;
  1029.    c(43) = a43;
  1030.  
  1031.    #undef a
  1032.    #undef b
  1033. }
  1034.  
  1035. //surgeon: added scaling, ortho and perspective matrices
  1036. /*
  1037. ** Matrix B is a scaling matrix
  1038. ** Multiply matrix A by float field, storing the result in matrix C
  1039. */
  1040. INLINE void m_MatMultBScale(Matrix *pA, float *pB, Matrix *pC)
  1041. {
  1042.    #define a(x) (pA->v[OF_##x])
  1043.    #define b(x) (pB[OF_##x])
  1044.    #define c(x) (pC->v[OF_##x])
  1045.  
  1046.    float a11 = a(11),  b11 = b(11);
  1047.    float a12 = a(12);
  1048.    float a13 = a(13);
  1049.    float a14 = a(14);
  1050.    float a21 = a(21);
  1051.    float a22 = a(22),  b22 = b(22);
  1052.    float a23 = a(23);
  1053.    float a24 = a(24);
  1054.    float a31 = a(31);
  1055.    float a32 = a(32);
  1056.    float a33 = a(33),  b33 = b(33);
  1057.    float a34 = a(34);
  1058.    float a41 = a(41);
  1059.    float a42 = a(42);
  1060.    float a43 = a(43);
  1061.    float a44 = a(44);
  1062.  
  1063. //surgeon: order switched to preserve registers
  1064.  
  1065.    c(11) = b11*a11;
  1066.    c(21) = b11*a21;
  1067.    c(31) = b11*a31;
  1068.    c(41) = b11*a41;
  1069.  
  1070.    c(12) = b22*a12;
  1071.    c(22) = b22*a22;
  1072.    c(32) = b22*a32;
  1073.    c(42) = b22*a42;
  1074.  
  1075.    c(13) = b33*a13;
  1076.    c(23) = b33*a23;
  1077.    c(33) = b33*a33;
  1078.    c(43) = b33*a43;
  1079.  
  1080.    c(14) = a14;
  1081.    c(24) = a24;
  1082.    c(34) = a34;
  1083.    c(44) = a44;
  1084.  
  1085.    #undef a
  1086.    #undef b
  1087. }
  1088. /*
  1089. ** Matrix B is a orthogonal matrix
  1090. ** Multiply matrix A by float field, storing the result in matrix C
  1091. */
  1092. INLINE void m_MatMultBOrtho(Matrix *pA, float *pB, Matrix *pC)
  1093. {
  1094.    #define a(x) (pA->v[OF_##x])
  1095.    #define b(x) (pB[OF_##x])
  1096.    #define c(x) (pC->v[OF_##x])
  1097.  
  1098.    float a11 = a(11),  b11 = b(11);
  1099.    float a12 = a(12);
  1100.    float a13 = a(13);
  1101.    float a14 = a(14),  b14 = b(14);
  1102.    float a21 = a(21);
  1103.    float a22 = a(22),  b22 = b(22);
  1104.    float a23 = a(23);
  1105.    float a24 = a(24),  b24 = b(24);
  1106.    float a31 = a(31);
  1107.    float a32 = a(32);
  1108.    float a33 = a(33),  b33 = b(33);
  1109.    float a34 = a(34),  b34 = b(34);
  1110.    float a41 = a(41);
  1111.    float a42 = a(42);
  1112.    float a43 = a(43);
  1113.    float a44 = a(44);
  1114.  
  1115. //surgeon: pipelining
  1116.  
  1117.    c(11) = b11*a11;
  1118.    c(21) = b11*a21;
  1119.    c(31) = b11*a31;
  1120.    c(41) = b11*a41;
  1121.  
  1122.    c(12) = b22*a12;
  1123.    c(22) = b22*a22;
  1124.    c(32) = b22*a32;
  1125.    c(42) = b22*a42;
  1126.  
  1127.    c(13) = b33*a13;
  1128.    c(23) = b33*a23;
  1129.    c(33) = b33*a33;
  1130.    c(43) = b33*a43;
  1131.  
  1132. a14 += b34*a13;
  1133. a24 += b34*a23;
  1134. a34 += b34*a33;
  1135. a44 += b34*a43;
  1136.  
  1137. a14 += b24*a12;
  1138. a24 += b24*a22;
  1139. a34 += b24*a32;
  1140. a44 += b24*a42;
  1141.  
  1142. a14 += b14*a11;
  1143. a24 += b14*a21;
  1144. a34 += b14*a31;
  1145. a44 += b14*a41;
  1146.  
  1147. c(14) = a14; 
  1148. c(24) = a24; 
  1149. c(34) = a34; 
  1150. c(44) = a44; 
  1151.  
  1152.    #undef a
  1153.    #undef b
  1154. }
  1155.  
  1156. INLINE void m_MatMultBPersp(Matrix *pA, float *pB, Matrix *pC)
  1157. {
  1158.    #define a(x) (pA->v[OF_##x])
  1159.    #define b(x) (pB[OF_##x])
  1160.    #define c(x) (pC->v[OF_##x])
  1161.  
  1162.    float a11 = a(11),  b11 = b(11);
  1163.    float a12 = a(12);
  1164.    float a13 = a(13),  b13 = b(13);
  1165.    float a14 = -a(14);
  1166.    float a21 = a(21);
  1167.    float a22 = a(22),  b22 = b(22);
  1168.    float a23 = a(23),  b23 = b(23);
  1169.    float a24 = -a(24);
  1170.    float a31 = a(31);
  1171.    float a32 = a(32);
  1172.    float a33 = a(33),  b33 = b(33);
  1173.    float a34 = -a(34),  b34 = b(34);
  1174.    float a41 = a(41);
  1175.    float a42 = a(42);
  1176.    float a43 = a(43);
  1177.    float a44 = -a(44);
  1178.  
  1179.  
  1180.    c(11) = b11*a11;
  1181.    c(21) = b11*a21;
  1182.    c(31) = b11*a31;
  1183.    c(41) = b11*a41;
  1184.  
  1185.    c(12) = b22*a12;
  1186.    c(22) = b22*a22;
  1187.    c(32) = b22*a32;
  1188.    c(42) = b22*a42;
  1189.  
  1190.    c(14) = b34*a13;
  1191.    c(24) = b34*a23;
  1192.    c(34) = b34*a33;
  1193.    c(44) = b34*a43;
  1194.  
  1195. a14 += b33*a13;
  1196. a24 += b33*a23;
  1197. a34 += b33*a33;
  1198. a44 += b33*a43;
  1199.  
  1200. a14 += b23*a12;
  1201. a24 += b23*a22;
  1202. a34 += b23*a32;
  1203. a44 += b23*a42;
  1204.  
  1205. a14 += b13*a11;
  1206. a24 += b13*a21;
  1207. a34 += b13*a31;
  1208. a44 += b13*a41;
  1209.  
  1210. c(13) = a14; 
  1211. c(23) = a24; 
  1212. c(33) = a34; 
  1213. c(43) = a44; 
  1214.  
  1215.  
  1216.    #undef a
  1217.    #undef b
  1218. }
  1219.  
  1220. //surgeon begin -->
  1221. /*
  1222. ** Matrix B is a translation matrix
  1223. ** Matrix A has 3 rows
  1224. ** Multiply matrix A by float field, storing the result in matrix C
  1225. */
  1226. INLINE void m_MatMultA0001_BTrans(Matrix *pA, float *pB, Matrix *pC)
  1227. {
  1228.    #define a(x) (pA->v[OF_##x])
  1229.    #define b(x) (pB[OF_##x])
  1230.    #define c(x) (pC->v[OF_##x])
  1231.  
  1232.    float a11 = a(11);
  1233.    float a12 = a(12);
  1234.    float a13 = a(13);
  1235.    float a14 = a(14),  b14 = b(14);
  1236.    float a21 = a(21);
  1237.    float a22 = a(22);
  1238.    float a23 = a(23);
  1239.    float a24 = a(24),  b24 = b(24);
  1240.    float a31 = a(31);
  1241.    float a32 = a(32);
  1242.    float a33 = a(33);
  1243.    float a34 = a(34),  b34 = b(34);
  1244.  
  1245.  
  1246.  
  1247. a14 += b34*a13;
  1248. a24 += b34*a23;
  1249. a34 += b34*a33;
  1250.  
  1251. a14 += b24*a12;
  1252. a24 += b24*a22;
  1253. a34 += b24*a32;
  1254.  
  1255. a14 += b14*a11;
  1256. a24 += b14*a21;
  1257. a34 += b14*a31;
  1258.  
  1259. c(14) = a14; 
  1260. c(24) = a24; 
  1261. c(34) = a34; 
  1262. c(44) = 1.f; 
  1263.  
  1264.    c(11) = a11;
  1265.    c(12) = a12;
  1266.    c(13) = a13;
  1267.  
  1268.    c(21) = a21;
  1269.    c(22) = a22;
  1270.    c(23) = a23;
  1271.  
  1272.    c(31) = a31;
  1273.    c(32) = a32;
  1274.    c(33) = a33;
  1275.  
  1276.    c(41) = 0.f;
  1277.    c(42) = 0.f;
  1278.    c(43) = 0.f;
  1279.  
  1280.    #undef a
  1281.    #undef b
  1282. }
  1283.  
  1284. //surgeon: added scaling, ortho and perspective matrices
  1285. /*
  1286. ** Matrix B is a scaling matrix
  1287. ** Matrix A has 3 rows
  1288. ** Multiply matrix A by float field, storing the result in matrix C
  1289. */
  1290. INLINE void m_MatMultA0001_BScale(Matrix *pA, float *pB, Matrix *pC)
  1291. {
  1292.    #define a(x) (pA->v[OF_##x])
  1293.    #define b(x) (pB[OF_##x])
  1294.    #define c(x) (pC->v[OF_##x])
  1295.  
  1296.    float a11 = a(11),  b11 = b(11);
  1297.    float a12 = a(12);
  1298.    float a13 = a(13);
  1299.    float a14 = a(14);
  1300.    float a21 = a(21);
  1301.    float a22 = a(22),  b22 = b(22);
  1302.    float a23 = a(23);
  1303.    float a24 = a(24);
  1304.    float a31 = a(31);
  1305.    float a32 = a(32);
  1306.    float a33 = a(33),  b33 = b(33);
  1307.    float a34 = a(34);
  1308.  
  1309.  
  1310.    c(11) = b11*a11;
  1311.    c(21) = b11*a21;
  1312.    c(31) = b11*a31;
  1313.    c(41) = 0.f;
  1314.  
  1315.    c(12) = b22*a12;
  1316.    c(22) = b22*a22;
  1317.    c(32) = b22*a32;
  1318.    c(42) = 0.f;
  1319.  
  1320.    c(13) = b33*a13;
  1321.    c(23) = b33*a23;
  1322.    c(33) = b33*a33;
  1323.    c(43) = 0.f;
  1324.  
  1325.    c(14) = a14;
  1326.    c(24) = a24;
  1327.    c(34) = a34;
  1328.    c(44) = 1.f;
  1329.  
  1330.    #undef a
  1331.    #undef b
  1332. }
  1333.  
  1334. /*
  1335. ** Matrix B is a orthogonal matrix
  1336. ** Matrix A has 3 rows
  1337. ** Multiply matrix A by float field, storing the result in matrix C
  1338. */
  1339. INLINE void m_MatMultA0001_BOrtho(Matrix *pA, float *pB, Matrix *pC)
  1340. {
  1341.    #define a(x) (pA->v[OF_##x])
  1342.    #define b(x) (pB[OF_##x])
  1343.    #define c(x) (pC->v[OF_##x])
  1344.  
  1345.    float a11 = a(11),  b11 = b(11);
  1346.    float a12 = a(12);
  1347.    float a13 = a(13);
  1348.    float a14 = a(14),  b14 = b(14);
  1349.    float a21 = a(21);
  1350.    float a22 = a(22),  b22 = b(22);
  1351.    float a23 = a(23);
  1352.    float a24 = a(24),  b24 = b(24);
  1353.    float a31 = a(31);
  1354.    float a32 = a(32);
  1355.    float a33 = a(33),  b33 = b(33);
  1356.    float a34 = a(34),  b34 = b(34);
  1357.  
  1358.  
  1359.    c(11) = b11*a11;
  1360.    c(21) = b11*a21;
  1361.    c(31) = b11*a31;
  1362.    c(41) = 0.f;
  1363.  
  1364.    c(12) = b22*a12;
  1365.    c(22) = b22*a22;
  1366.    c(32) = b22*a32;
  1367.    c(42) = 0.f;
  1368.  
  1369.    c(13) = b33*a13;
  1370.    c(23) = b33*a23;
  1371.    c(33) = b33*a33;
  1372.    c(43) = 0.f;
  1373.  
  1374. a14 += b34*a13;
  1375. a24 += b34*a23;
  1376. a34 += b34*a33;
  1377.  
  1378. a14 += b24*a12;
  1379. a24 += b24*a22;
  1380. a34 += b24*a32;
  1381.  
  1382. a14 += b14*a11;
  1383. a24 += b14*a21;
  1384. a34 += b14*a31;
  1385.  
  1386. c(14) = a14; 
  1387. c(24) = a24; 
  1388. c(34) = a34; 
  1389. c(44) = 1.f; 
  1390.  
  1391.    #undef a
  1392.    #undef b
  1393. }
  1394.  
  1395. INLINE void m_MatMultA0001_BPersp(Matrix *pA, float *pB, Matrix *pC)
  1396. {
  1397.    #define a(x) (pA->v[OF_##x])
  1398.    #define b(x) (pB[OF_##x])
  1399.    #define c(x) (pC->v[OF_##x])
  1400.  
  1401.    float a11 = a(11),  b11 = b(11);
  1402.    float a12 = a(12);
  1403.    float a13 = a(13),  b13 = b(13);
  1404.    float a14 = -a(14);
  1405.    float a21 = a(21);
  1406.    float a22 = a(22),  b22 = b(22);
  1407.    float a23 = a(23),  b23 = b(23);
  1408.    float a24 = -a(24);
  1409.    float a31 = a(31);
  1410.    float a32 = a(32);
  1411.    float a33 = a(33),  b33 = b(33);
  1412.    float a34 = -a(34),  b34 = b(34);
  1413.    float a41 = a(41);
  1414.    float a42 = a(42);
  1415.    float a43 = a(43);
  1416.    float a44 = -a(44);
  1417.  
  1418.    c(11) = b11*a11;
  1419.    c(21) = b11*a21;
  1420.    c(31) = b11*a31;
  1421.    c(41) = 0.f;
  1422.  
  1423.    c(12) = b22*a12;
  1424.    c(22) = b22*a22;
  1425.    c(32) = b22*a32;
  1426.    c(42) = 0.f;
  1427.  
  1428.    c(14) = b34*a13;
  1429.    c(24) = b34*a23;
  1430.    c(34) = b34*a33;
  1431.    c(44) = 0.f;
  1432.  
  1433. a14 += b33*a13;
  1434. a24 += b33*a23;
  1435. a34 += b33*a33;
  1436.  
  1437. a14 += b23*a12;
  1438. a24 += b23*a22;
  1439. a34 += b23*a32;
  1440.  
  1441. a14 += b13*a11;
  1442. a24 += b13*a21;
  1443. a34 += b13*a31;
  1444.  
  1445. c(13) = a14; 
  1446. c(23) = a24; 
  1447. c(33) = a34; 
  1448. c(43) = -1; 
  1449.  
  1450.  
  1451.    #undef a
  1452.    #undef b
  1453. }
  1454.  
  1455. //surgeon end <--
  1456.  
  1457. INLINE void m_LoadMatrixf(Matrix *pA, const float *v)
  1458. {
  1459.    #define a(x) (pA->v[OF_##x] = *v++)
  1460.  
  1461.    a(11); a(21); a(31); a(41);
  1462.    a(12); a(22); a(32); a(42);
  1463.    a(13); a(23); a(33); a(43);
  1464.    a(14); a(24); a(34); a(44);
  1465.  
  1466.    if (*(v-4) == 0.f && *(v-3) == 0.f && *(v-2) == 0.f && *(v-1) == 1.f)
  1467.       pA->flags = MGLMAT_0001;
  1468.    else
  1469.       pA->flags = MGLMAT_UNKNOWN;
  1470.    #undef a
  1471. }
  1472.  
  1473. INLINE void m_LoadMatrixd(Matrix *pA, const double *v)
  1474. {
  1475.    #define a(x) (pA->v[OF_##x] = (float)*v++)
  1476.  
  1477.    a(11); a(21); a(31); a(41);
  1478.    a(12); a(22); a(32); a(42);
  1479.    a(13); a(23); a(33); a(43);
  1480.    a(14); a(24); a(34); a(44);
  1481.  
  1482.    if (*(v-4) == 0.0 && *(v-3) == 0.0 && *(v-2) == 0.0 && *(v-1) == 1.0)
  1483.       pA->flags = MGLMAT_0001;
  1484.    else
  1485.       pA->flags = MGLMAT_UNKNOWN;
  1486.  
  1487.    #undef a
  1488. }
  1489.  
  1490.  
  1491. INLINE void m_LoadIdentity(Matrix *pA)
  1492. {
  1493.    #define a(x) (pA->v[OF_##x] = 0.f)
  1494.    #define b(x) (pA->v[OF_##x] = 1.f)
  1495.  
  1496.    b(11); a(21); a(31); a(41);
  1497.    a(12); b(22); a(32); a(42);
  1498.    a(13); a(23); b(33); a(43);
  1499.    a(14); a(24); a(34); b(44);
  1500.  
  1501.    pA->flags = MGLMAT_IDENTITY;
  1502.  
  1503.    #undef a
  1504.    #undef b
  1505. }
  1506.  
  1507. /*
  1508. ** Multiply matrix A by the matrix passed by v.
  1509. ** The vtype specifies what kind of matrix v points to, so that
  1510. ** this routine may select an optimized matrix multiplication routine.
  1511. */
  1512.  
  1513. INLINE void m_Mult(Matrix *pA, float *v, int vtype, Matrix *pC)
  1514. {
  1515.  
  1516.     if(pA->flags == MGLMAT_IDENTITY)
  1517.     {
  1518.         m_MatMultAIdent(v, pC);
  1519.         pC->flags = vtype;
  1520.         return;
  1521.     }
  1522.  
  1523.     if(pA->flags & (MGLMASK_NONE | MGLMAT_PERSPECTIVE)) 
  1524.     {
  1525.     switch (vtype)
  1526.     {
  1527.     case MGLMAT_ROT100:
  1528.         m_MatMultBRot100(pA, v, pC);
  1529.         break;
  1530.  
  1531.     case MGLMAT_ROT001:
  1532.         m_MatMultBRot001(pA, v, pC);
  1533.         break;
  1534.  
  1535.     case MGLMAT_ROT010:
  1536.         m_MatMultBRot010(pA, v, pC);
  1537.         break;
  1538.  
  1539.     case MGLMAT_TRANSLATION:
  1540.         m_MatMultBTrans(pA, v, pC);
  1541.         break;
  1542.  
  1543.     case MGLMAT_GENERAL_SCALE:
  1544.         m_MatMultBScale(pA, v, pC);
  1545.         break;
  1546.  
  1547.     case MGLMAT_PERSPECTIVE:
  1548.         m_MatMultBPersp(pA, v, pC);
  1549.         break;
  1550.  
  1551.     case MGLMAT_ORTHO:
  1552.         m_MatMultBOrtho(pA, v, pC);
  1553.         break;
  1554.  
  1555.     case MGLMAT_0001:
  1556.     case MGLMAT_ROTATION:
  1557.         {
  1558.         if(pA->flags & MGLMAT_PERSPECTIVE)
  1559.             m_MatMultAPerspB0001(pA, v, pC);
  1560.         else
  1561.             m_MatMultB0001(pA, v, pC);
  1562.         break;
  1563.         }
  1564.  
  1565.     default:
  1566.         switch (pA->flags)
  1567.         {
  1568.         case MGLMAT_TRANSLATION:
  1569.             m_MatMultATrans(pA, v, pC);
  1570.             break;
  1571.  
  1572.         case MGLMAT_0001:
  1573.         case MGLMAT_ROTATION:
  1574.             m_MatMultA0001(pA, v, pC);
  1575.             break;
  1576.         default: //MGLMASK_UNKNOWN
  1577.             m_MatMultGeneral(pA, v, pC);
  1578.             break;
  1579.         }
  1580.     break;
  1581.     }
  1582.     }
  1583.     else //pA = MGLMASK_0001
  1584.     {
  1585.     switch (vtype)
  1586.     {
  1587.     case MGLMAT_ROT100:
  1588.         m_MatMultA0001_BRot100(pA, v, pC);
  1589.         break;
  1590.  
  1591.     case MGLMAT_ROT001:
  1592.         m_MatMultA0001_BRot001(pA, v, pC);
  1593.         break;
  1594.  
  1595.     case MGLMAT_ROT010:
  1596.         m_MatMultA0001_BRot010(pA, v, pC);
  1597.         break;
  1598.  
  1599.     case MGLMAT_TRANSLATION:
  1600.         m_MatMultA0001_BTrans(pA, v, pC);
  1601.         break;
  1602.  
  1603.     case MGLMAT_GENERAL_SCALE:
  1604.         m_MatMultA0001_BScale(pA, v, pC);
  1605.         break;
  1606.  
  1607.     case MGLMAT_PERSPECTIVE:
  1608.         m_MatMultA0001_BPersp(pA, v, pC);
  1609.         break;
  1610.  
  1611.     case MGLMAT_ORTHO:
  1612.         m_MatMultA0001_BOrtho(pA, v, pC);
  1613.         break;
  1614.  
  1615.     case MGLMAT_0001:
  1616.     case MGLMAT_ROTATION:
  1617.         m_MatMultA0001_B0001(pA, v, pC);
  1618.         break;
  1619.  
  1620.     default:
  1621.         m_MatMultA0001(pA, v, pC);
  1622.         break;
  1623.     }
  1624.  
  1625.     }
  1626.  
  1627.     pC->flags = MGLMAT_UNKNOWN;
  1628.  
  1629. #if 0 //not used
  1630.  
  1631.     if ((vtype == MGLMAT_PERSPECTIVE) && (pA->flags & MGLMASK_0001))
  1632.     {
  1633.     pC->flags = MGLMAT_00NEG10;
  1634.     return;
  1635.     }
  1636.  
  1637. #endif
  1638.  
  1639.     if (!(vtype & MGLMASK_NONE))
  1640.     {
  1641.         if (!(pA->flags & MGLMASK_NONE))
  1642.         pC->flags = MGLMAT_0001;
  1643.        }
  1644.  
  1645. }
  1646.  
  1647.  
  1648. void m_CombineMatrices(GLcontext context)
  1649. {
  1650.   context->CombinedValid = GL_TRUE;
  1651.  
  1652.   m_Mult(CurrentP, CurrentMV->v, CurrentMV->flags,                &(context->CombinedMatrix));
  1653.  
  1654. }
  1655.  
  1656.  
  1657. void m_PrintMatrix(Matrix *pA)
  1658. {
  1659. #ifndef NDEBUG
  1660.    #define a(x) (pA->v[OF_##x])
  1661.    printf("Matrix at 0x%lX\n", (ULONG)pA);
  1662.    printf("    | %3.6f %3.6f %3.6f %3.6f |\n",
  1663.       a(11), a(12), a(13), a(14));
  1664.    printf("    | %3.6f %3.6f %3.6f %3.6f |\n",
  1665.       a(21), a(22), a(23), a(24));
  1666.    printf("A = | %3.6f %3.6f %3.6f %3.6f |\n",
  1667.       a(31), a(32), a(33), a(34));
  1668.    printf("    | %3.6f %3.6f %3.6f %3.6f |\n",
  1669.       a(41), a(42), a(43), a(44));
  1670.    #undef a
  1671. #endif
  1672. }
  1673.  
  1674. // Interface functions
  1675. void GLMatrixMode(GLcontext context, GLenum mode)
  1676. {
  1677.    //LOG(3, glMatrixMode, "%d", mode);
  1678.    GLASSERT(mode == GL_PROJECTION || mode == GL_MODELVIEW);
  1679.    GLASSERT(context != NULL);
  1680.  
  1681.    context->CurrentMatrixMode = mode;
  1682. }
  1683.  
  1684. void GLLoadIdentity(GLcontext context)
  1685. {
  1686.    //LOG(3, glLoadIdentity, "");
  1687.    GLASSERT(context != NULL);
  1688.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1689.    if (context->CurrentMatrixMode == GL_MODELVIEW)
  1690.    {
  1691.       m_LoadIdentity(CurrentMV);
  1692.    }
  1693.    else
  1694.    {
  1695.       m_LoadIdentity(CurrentP);
  1696.    }
  1697.    context->CombinedValid = GL_FALSE;
  1698.    context->InvRotValid = GL_FALSE;
  1699. }
  1700.  
  1701. void MGLPrintMatrix(GLcontext context, int mode)
  1702. {
  1703. #ifndef NDEBUG
  1704.    if (mode == GL_MODELVIEW)
  1705.    {
  1706.       m_PrintMatrix(CurrentMV);
  1707.    }
  1708.    else
  1709.    {
  1710.       m_PrintMatrix(CurrentP);
  1711.    }
  1712.    printf("\n");
  1713. #endif
  1714. }
  1715.  
  1716. void MGLPrintMatrixStack(GLcontext context, int mode)
  1717. {
  1718. #ifndef NDEBUG
  1719.    int i;
  1720.  
  1721.    printf("Stack Top:\n");
  1722.    MGLPrintMatrix(context, mode);
  1723.    printf("Rest of stack:\n");
  1724.  
  1725.    if (mode == GL_MODELVIEW) {
  1726.       if (context->ModelViewStackPointer == 0) {
  1727.          printf("Empty\n\n\n");
  1728.          return;
  1729.       }
  1730.       for (i=context->ModelViewStackPointer-1; i>=0; i--)
  1731.       {
  1732.          printf("%d:\n", i);
  1733.          m_PrintMatrix(&(context->ModelViewStack[i]));
  1734.       }
  1735.    }
  1736.    else
  1737.    {
  1738.       if (context->ProjectionStackPointer == 0) {
  1739.          printf("Empty\n\n\n");
  1740.          return;
  1741.       }
  1742.       for (i=context->ProjectionStackPointer-1; i>=0; i--)
  1743.       {
  1744.          printf("%d:\n", i);
  1745.          m_PrintMatrix(&(context->ProjectionStack[i]));
  1746.       }
  1747.    }
  1748.    printf("\n\n");
  1749. #endif
  1750. }
  1751.  
  1752.  
  1753. void GLLoadMatrixf(GLcontext context, const GLfloat *m)
  1754. {
  1755.    //LOG(3, glLoadMatrixf, "%f %f %f ...", *m, *(m+1), *(m+2));
  1756.    GLASSERT(context != NULL);
  1757.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1758.    
  1759.    context->CombinedValid = GL_FALSE;
  1760.    context->InvRotValid = GL_FALSE;
  1761.    m_LoadMatrixf(CMATRIX(context), m);
  1762. }
  1763.  
  1764. void GLLoadMatrixd(GLcontext context, const GLdouble *m)
  1765. {
  1766.    //LOG(3, glLoadMatrixf, "%lf %lf %lf ...", *m, *(m+1), *(m+2));
  1767.    GLASSERT(context != NULL);
  1768.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1769.    
  1770.    context->CombinedValid = GL_FALSE;
  1771.    context->InvRotValid = GL_FALSE;
  1772.    m_LoadMatrixd(CMATRIX(context), m);
  1773. }
  1774.  
  1775. void GLPushMatrix(GLcontext context)
  1776. {
  1777.    //LOG(3, glPushMatrix, "");
  1778.    GLASSERT(context != NULL);
  1779.    GLASSERT(context->ProjectionStackPointer <= PROJECTION_STACK_SIZE);
  1780.    GLASSERT(context->ModelViewStackPointer  <= MODELVIEW_STACK_SIZE);
  1781.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1782.  
  1783.    if (context->CurrentMatrixMode == GL_PROJECTION)
  1784.    {
  1785.       m_MatCopy(&(context->ProjectionStack[context->ProjectionStackPointer]),
  1786.          CurrentP);
  1787.       context->ProjectionStackPointer ++;
  1788.    }
  1789.    else
  1790.    {
  1791.       m_MatCopy(&(context->ModelViewStack[context->ModelViewStackPointer]),
  1792.          CurrentMV);
  1793.       context->ModelViewStackPointer ++;
  1794.    }
  1795. }
  1796.  
  1797. void GLPopMatrix(GLcontext context)
  1798. {
  1799.    //LOG(3, glPopMatrix, "");
  1800.    GLASSERT(context != NULL);
  1801.    GLASSERT(context->ProjectionStackPointer >= 0);
  1802.    GLASSERT(context->ModelViewStackPointer  >= 0);
  1803.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1804.    context->InvRotValid = GL_FALSE;
  1805.    context->CombinedValid = GL_FALSE;
  1806.  
  1807.    if (context->CurrentMatrixMode == GL_PROJECTION)
  1808.    {
  1809.       if (context->ProjectionStackPointer <= 0)
  1810.       {
  1811.          context->CurrentError = GL_INVALID_OPERATION;
  1812.          return;
  1813.       }
  1814.       context->ProjectionStackPointer --;
  1815.       m_MatCopy(CurrentP,
  1816.          &(context->ProjectionStack[context->ProjectionStackPointer]));
  1817.    }
  1818.    else
  1819.    {
  1820.       if (context->ModelViewStackPointer <= 0)
  1821.       {
  1822.          context->CurrentError = GL_INVALID_OPERATION;
  1823.          return;
  1824.       }
  1825.       context->ModelViewStackPointer --;
  1826.       m_MatCopy(CurrentMV,
  1827.          &(context->ModelViewStack[context->ModelViewStackPointer]));
  1828.    }
  1829.  
  1830. }
  1831.  
  1832. void GLFrustum(GLcontext context, GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
  1833. {
  1834.  
  1835. /*
  1836. layout:
  1837.         X  0  X  0
  1838.  
  1839.         0  X  X  0
  1840.  
  1841.         0  0  X  X
  1842.  
  1843.         0  0 -1  0
  1844. */
  1845.  
  1846.    float v[16];
  1847.    float n2 = 2.0*zNear;
  1848.    float rli = 1.f / (float)(right-left);
  1849.    float tbi = 1.f / (float)(top-bottom);
  1850.    float fni = 1.f / (float)(zFar-zNear);
  1851.  
  1852.    //LOG(3, glFrustum, "%lf &lf %lf %lf %lf %lf", left, right, bottom, top, zNear, zFar);
  1853.  
  1854.    GLASSERT(context != NULL);
  1855.    GLFlagError(context, zFar<=0.0 || zNear <=0.0, GL_INVALID_VALUE);
  1856.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1857.    context->InvRotValid = GL_FALSE;
  1858.    context->CombinedValid = GL_FALSE;
  1859.  
  1860.    v[OF_11] = n2*rli; v[OF_21] = 0.0;
  1861.    v[OF_31] = 0.0; v[OF_41] = 0.0;
  1862.  
  1863.    v[OF_22] = n2*tbi;
  1864.    v[OF_12] = 0.0; v[OF_32] = 0.0; v[OF_42] = 0.0;
  1865.  
  1866.    v[OF_13] = (right+left)*rli; v[OF_23] = (top+bottom)*tbi;
  1867.    v[OF_33] = -(zFar+zNear)*fni;
  1868.    v[OF_43] = -1.0;
  1869.  
  1870.    v[OF_14] = 0.0; v[OF_24] = 0.0;
  1871.    v[OF_34] = -(2.0*zFar*zNear)*fni;
  1872.    v[OF_44] = 0.0;
  1873.  
  1874.    m_Mult(CMATRIX(context),
  1875.       v,
  1876.       MGLMAT_PERSPECTIVE,
  1877.       OMATRIX(context)
  1878.       );
  1879.    SMATRIX(context);
  1880. /*
  1881.    m_MatMultBPersp(CMATRIX(context), v, OMATRIX(context));
  1882.    SMATRIX(context);
  1883. */
  1884. }
  1885.  
  1886. void GLOrtho(GLcontext context, GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
  1887. {
  1888.  
  1889. /*
  1890. layout:
  1891.         X  0  0  X
  1892.  
  1893.         0  X  0  X
  1894.  
  1895.         0  0  X  X
  1896.  
  1897.         0  0  0  1
  1898. */
  1899.  
  1900.    float rli = 1.0  / (right-left);
  1901.    float tbi = 1.0  / (top-bottom);
  1902.    float fni = 1.0 / (zFar-zNear);
  1903.    float v[16];
  1904.  
  1905.    //LOG(3, glOrtho, "%lf %lf %lf %lf %lf %lf", left, right, bottom, top, zNear, zFar);
  1906.  
  1907.    GLASSERT(context != NULL);
  1908.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1909.    context->InvRotValid = GL_FALSE;
  1910.    context->CombinedValid = GL_FALSE;
  1911.  
  1912.    v[OF_11] = 2.0*rli;
  1913.    v[OF_12] = 0.0; v[OF_13] = 0.0;
  1914.    v[OF_14] = -(right+left)*rli;
  1915.  
  1916.    v[OF_21] = 0.0;
  1917.    v[OF_22] = 2.0*tbi;
  1918.    v[OF_23] = 0.0;
  1919.    v[OF_24] = -(top+bottom)*tbi;
  1920.  
  1921.    v[OF_31] = 0.0;
  1922.    v[OF_32] = 0.0;
  1923.    v[OF_33] = -2.0*fni; v[OF_34] = -(zFar+zNear)*fni;
  1924.  
  1925.    v[OF_41] = v[OF_42] = v[OF_43] = 0.0;
  1926.    v[OF_44] = 1.0;
  1927.  
  1928.    m_Mult(CMATRIX(context), v, MGLMAT_ORTHO, OMATRIX(context));
  1929.    SMATRIX(context);
  1930. /*
  1931.    m_MatMultBOrtho(CMATRIX(context), v, OMATRIX(context));
  1932.    SMATRIX(context);
  1933. */
  1934. }
  1935.  
  1936. void GLMultMatrixf(GLcontext context, const GLfloat *mat)
  1937. {
  1938.    //LOG(3, glMultMatrixf, "%f %f %f ...", *mat, *(mat+1), *(mat+2));
  1939.    GLASSERT(context != NULL);
  1940.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1941.    context->InvRotValid = GL_FALSE;
  1942.    context->CombinedValid = GL_FALSE;
  1943.  
  1944. //   m_Mult(CMATRIX(context), (float *)mat, 0, OMATRIX(context));
  1945.    m_Mult(CMATRIX(context), (float *)mat, MGLMAT_UNKNOWN, OMATRIX(context));
  1946.    SMATRIX(context);
  1947. }
  1948.  
  1949. void GLMultMatrixd(GLcontext context, const GLdouble *mat)
  1950. {
  1951.    GLfloat v[16];
  1952.    int i;
  1953.    //LOG(3, glMultMatrixd, "%lf %lf %lf ...", *mat, *(mat+1), *(mat+2));
  1954.  
  1955.    GLASSERT(context != NULL);
  1956.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1957.    context->InvRotValid = GL_FALSE;
  1958.    context->CombinedValid = GL_FALSE;
  1959.  
  1960.    for (i=0; i<16; i++)
  1961.    {
  1962.         v[i] = (GLfloat) *mat++;
  1963.    }
  1964.  
  1965. //   m_Mult(CMATRIX(context), v, 0, OMATRIX(context));
  1966.    m_Mult(CMATRIX(context), v, MGLMAT_UNKNOWN, OMATRIX(context));
  1967.    SMATRIX(context);
  1968. }
  1969.  
  1970. /*
  1971. ** The following routine was ripped from the Mesa source code.
  1972. ** I did use this because it is much better than my original design. The code
  1973. ** is just modified a bit to adapt to my code layout, but otherwise is unmodified.
  1974. ** This code is included with the kind permission of Brian Paul, the author of Mesa.
  1975. */
  1976.  
  1977.  
  1978. void GLRotatef(GLcontext context, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
  1979. {
  1980.  
  1981. /*
  1982. layout:
  1983. x x x 0
  1984. x x x 0
  1985. x x x 0
  1986. 0 0 0 1
  1987. */
  1988.  
  1989.    float v[16];
  1990.    float mag, s, c;
  1991.    float xx,yy,zz,xy,yz,zx,xs,ys,zs,one_c;
  1992.  
  1993.    #define v(x) (v[OF_##x])
  1994.  
  1995.    //LOG(3, glRotatef, "%f %f %f %f", angle, x,y,z);
  1996.  
  1997.    GLASSERT(context != NULL);
  1998.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  1999.    context->InvRotValid = GL_FALSE;
  2000.    context->CombinedValid = GL_FALSE;
  2001.  
  2002. //moved by surgeon
  2003. // s = (float)sin(angle * PI/180.0);
  2004. // c = (float)cos(angle * PI/180.0);
  2005. // mag = sqrt(x*x+y*y+z*z);
  2006.  
  2007.    xx = x*x;
  2008.    yy = y*y;
  2009.    zz = z*z;
  2010.    mag = sqrt(xx+yy+zz);
  2011.  
  2012.    if (mag == 0.0) return;
  2013.  
  2014.    if (mag != 1.0)
  2015.    {
  2016.       x /= mag;
  2017.       y /= mag;
  2018.       z /= mag;
  2019.    //surgeon
  2020.       xx = x*x;
  2021.       yy = y*y;
  2022.       zz = z*z;
  2023.    }
  2024.  
  2025. #ifdef TRIGTABLES
  2026.    s = (float)MGLSIN(angle);
  2027.    c = (float)MGLCOS(angle);
  2028. #else
  2029.    s = (float)sin(angle * PI/180.0);
  2030.    c = (float)cos(angle * PI/180.0);
  2031. #endif
  2032.  
  2033. // xx = x*x;
  2034. // yy = y*y;
  2035. // zz = z*z;
  2036.    xy = x*y;
  2037.    yz = y*z;
  2038.    zx = z*x;
  2039.    xs = x*s;
  2040.    ys = y*s;
  2041.    zs = z*s;
  2042.  
  2043.    one_c = 1.f - c;
  2044.  
  2045.    v(11) = one_c*xx + c;
  2046.    v(12) = one_c*xy - zs;
  2047.    v(13) = one_c*zx + ys;
  2048.  
  2049.    v(21) = one_c*xy + zs;
  2050.    v(22) = one_c*yy + c;
  2051.    v(23) = one_c*yz - xs;
  2052.  
  2053.    v(31) = one_c*zx - ys;
  2054.    v(32) = one_c*yz + xs;
  2055.    v(33) = one_c*zz + c;
  2056.  
  2057.    v(14) = v(24) = v(34) = 0.f;
  2058.    v(41) = 0.f;
  2059.    v(42) = 0.f;
  2060.    v(43) = 0.f;
  2061.    v(44) = 1.f;
  2062.  
  2063.    m_Mult(CMATRIX(context), v, MGLMAT_ROTATION, OMATRIX(context));
  2064.    SMATRIX(context);
  2065.    #undef v
  2066. }
  2067.  
  2068. void GLRotated(GLcontext context, GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  2069. {
  2070.    GLRotatef(context, (GLfloat)angle, (GLfloat)x, (GLfloat)y, (GLfloat)z);
  2071. }
  2072.  
  2073. /**********************************************************
  2074. Simplified ultra-fast rotation-routines by SuRgEoN
  2075. ***********************************************************/
  2076.  
  2077. void GLRotatefEXT(GLcontext context, GLfloat angle, const GLint xyz)
  2078. {
  2079.  
  2080. /*
  2081. layout:
  2082.  
  2083. 001:
  2084. x x 0 0
  2085. x x 0 0
  2086. 0 0 1 0
  2087. 0 0 0 1
  2088.  
  2089.  
  2090. 010:
  2091. x 0 x 0
  2092. 0 1 0 0
  2093. x 0 x 0
  2094. 0 0 0 1
  2095.  
  2096. 100:
  2097. 1 0 0 0
  2098. 0 x x 0
  2099. 0 x x 0
  2100. 0 0 0 1
  2101.  
  2102. */
  2103.  
  2104.    float c,s;
  2105.    float v[16];
  2106.  
  2107.    GLASSERT(context != NULL);
  2108.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  2109.    context->InvRotValid = GL_FALSE;
  2110.    context->CombinedValid = GL_FALSE;
  2111.  
  2112.    s = (float)sin(angle * PI/180.0);
  2113.    c = (float)cos(angle * PI/180.0);
  2114.  
  2115. #define v(x) (v[OF_##x])
  2116.  
  2117.    v(14) = v(24) = v(34) = 0.f;
  2118.    v(41) = 0.f;
  2119.    v(42) = 0.f;
  2120.    v(43) = 0.f;
  2121.    v(44) = 1.f;
  2122.  
  2123. if(xyz == GLROT_001)
  2124. {
  2125.    v(11) = c;
  2126.    v(12) = -s;
  2127.    v(13) = 0.f;
  2128.    v(21) = s;
  2129.    v(22) = c;
  2130.    v(23) = 0.f;
  2131.    v(31) = 0.f;
  2132.    v(32) = 0.f;
  2133.    v(33) = 1.f;
  2134. }
  2135. else if (xyz == GLROT_010)
  2136. {
  2137.    v(11) = c;
  2138.    v(12) = 0.f;
  2139.    v(13) = s;
  2140.    v(21) = 0.f;
  2141.    v(22) = 1.f;
  2142.    v(23) = 0.f;
  2143.    v(31) = -s;
  2144.    v(32) = 0.f;
  2145.    v(33) = c;
  2146.  
  2147. }
  2148. else //if (xyz == GLROT_100)
  2149. {
  2150.    v(11) = 1.f;
  2151.    v(12) = 0.f;
  2152.    v(13) = 0.f;
  2153.    v(21) = 0.f;
  2154.    v(22) = c;
  2155.    v(23) = -s;
  2156.    v(31) = 0.f;
  2157.    v(32) = s;
  2158.    v(33) = c;
  2159. }
  2160.  
  2161.  
  2162.    m_Mult(CMATRIX(context), v, xyz, OMATRIX(context));
  2163.    SMATRIX(context);
  2164. #undef v
  2165. }
  2166.  
  2167. void GLRotatefEXTs(GLcontext context, GLfloat sin_an, GLfloat cos_an, const GLint xyz)
  2168. {
  2169.  
  2170. /*
  2171. layout:
  2172.  
  2173. 001:
  2174. x x 0 0
  2175. x x 0 0
  2176. 0 0 1 0
  2177. 0 0 0 1
  2178.  
  2179.  
  2180. 010:
  2181. x 0 x 0
  2182. 0 1 0 0
  2183. x 0 x 0
  2184. 0 0 0 1
  2185.  
  2186. 100:
  2187. 1 0 0 0
  2188. 0 x x 0
  2189. 0 x x 0
  2190. 0 0 0 1
  2191.  
  2192. */
  2193. float v[16];
  2194.  
  2195.    GLASSERT(context != NULL);
  2196.    GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  2197.    context->InvRotValid = GL_FALSE;
  2198.    context->CombinedValid = GL_FALSE;
  2199.  
  2200. #define v(x) (v[OF_##x])
  2201.  
  2202.    v(14) = v(24) = v(34) = 0.f;
  2203.    v(41) = 0.f;
  2204.    v(42) = 0.f;
  2205.    v(43) = 0.f;
  2206.    v(44) = 1.f;
  2207.  
  2208. if(xyz == GLROT_001)
  2209. {
  2210.    v(11) = cos_an;
  2211.    v(12) = -sin_an;
  2212.    v(13) = 0.f;
  2213.    v(21) = sin_an;
  2214.    v(22) = cos_an;
  2215.    v(23) = 0.f;
  2216.    v(31) = 0.f;
  2217.    v(32) = 0.f;
  2218.    v(33) = 1.f;
  2219. }
  2220. else if (xyz == GLROT_010)
  2221. {
  2222.    v(11) = cos_an;
  2223.    v(12) = 0.f;
  2224.    v(13) = sin_an;
  2225.    v(21) = 0.f;
  2226.    v(22) = 1.f;
  2227.    v(23) = 0.f;
  2228.    v(31) = -sin_an;
  2229.    v(32) = 0.f;
  2230.    v(33) = cos_an;
  2231. }
  2232. else //if (xyz == GLROT_100)
  2233. {
  2234.    v(11) = 1.f;
  2235.    v(12) = 0.f;
  2236.    v(13) = 0.f;
  2237.    v(21) = 0.f;
  2238.    v(22) = cos_an;
  2239.    v(23) = -sin_an;
  2240.    v(31) = 0.f;
  2241.    v(32) = sin_an;
  2242.    v(33) = cos_an;
  2243. }
  2244.  
  2245.    m_Mult(CMATRIX(context), v, xyz, OMATRIX(context));
  2246.    SMATRIX(context);
  2247. #undef v
  2248. }
  2249.  
  2250. void GLScaled(GLcontext context, GLdouble x, GLdouble y, GLdouble z)
  2251. {
  2252.    float v[16];
  2253.    //LOG(3, glScaled, "%lf %lf %lf", x,y,z);
  2254.    GLASSERT(context!=NULL);
  2255.    context->InvRotValid = GL_FALSE;
  2256.    context->CombinedValid = GL_FALSE;
  2257.    #define v(x) (v[OF_##x])
  2258.    v(11) = (float)x;
  2259.  
  2260.    v(12) = 0.0; v(13) = 0.0; v(14) = 0.0;
  2261.    v(21) = 0.0;
  2262.  
  2263.    v(22) = (float)y;
  2264.  
  2265.    v(23) = 0.0; v(24) = 0.0;
  2266.    v(31) = 0.0; v(32) = 0.0;
  2267.  
  2268.    v(33) = (float)z;
  2269.  
  2270.    v(34) = 0.0;
  2271.    v(41) = 0.0; v(42) = 0.0; v(43) = 0.0; v(44) = 1.0;
  2272.  
  2273.    m_Mult(CMATRIX(context), v, MGLMAT_GENERAL_SCALE, OMATRIX(context));
  2274.    SMATRIX(context);
  2275. /*
  2276.    m_MatMultBScale(CMATRIX(context), v, OMATRIX(context));
  2277.    SMATRIX(context);
  2278. */
  2279.    #undef v
  2280. }
  2281.  
  2282. void GLScalef(GLcontext context, GLfloat x, GLfloat y, GLfloat z)
  2283. {
  2284.  
  2285. /*
  2286. layout:
  2287. x 0 0 0
  2288. 0 y 0 0
  2289. 0 0 z 0
  2290. 0 0 0 1
  2291. */
  2292.  
  2293.    float v[16];
  2294.    //LOG(3, glScalef, "%f %f %f", x,y,z);
  2295.    GLASSERT(context!=NULL);
  2296.    context->InvRotValid = GL_FALSE;
  2297.    context->CombinedValid = GL_FALSE;
  2298.    #define v(x) (v[OF_##x])
  2299.    v(11) = x;
  2300.  
  2301.    v(12) = 0.0; v(13) = 0.0; v(14) = 0.0;
  2302.    v(21) = 0.0;
  2303.  
  2304.    v(22) = y;
  2305.  
  2306.    v(23) = 0.0; v(24) = 0.0;
  2307.    v(31) = 0.0; v(32) = 0.0;
  2308.  
  2309.    v(33) = z;
  2310.  
  2311.    v(34) = 0.0;
  2312.    v(41) = 0.0; v(42) = 0.0; v(43) = 0.0; v(44) = 1.0;
  2313.  
  2314.    m_Mult(CMATRIX(context), v, MGLMAT_GENERAL_SCALE, OMATRIX(context));
  2315.    SMATRIX(context);
  2316. /*
  2317.    m_MatMultBScale(CMATRIX(context), v, OMATRIX(context));
  2318.    SMATRIX(context);
  2319. */
  2320.    #undef v
  2321. }
  2322.  
  2323. void GLTranslated(GLcontext context, GLdouble x, GLdouble y, GLdouble z)
  2324. {
  2325.    float v[16];
  2326.  
  2327.    //LOG(3, glTranslated, "%lf %lf %lf", x,y,z);
  2328.  
  2329.    GLASSERT(context != NULL);
  2330.    context->InvRotValid = GL_FALSE;
  2331.    context->CombinedValid = GL_FALSE;
  2332.  
  2333.    #define v(x) (v[OF_##x])
  2334.      v(11) = v(22) = v(33) = v(44) = 1.f;
  2335.      v(21) = v(31) = v(41) = v(12) = v(32) = v(42) = v(13) = v(23) = v(43) = 0.f;
  2336.  
  2337.  
  2338.    v(14) = (float)x; v(24) = (float)y; v(34) = (float)z;
  2339.  
  2340.    m_Mult(CMATRIX(context), v, MGLMAT_TRANSLATION, OMATRIX(context));
  2341.    SMATRIX(context);
  2342. /*
  2343.    m_MatMultBTrans(CMATRIX(context), v, OMATRIX(context));
  2344.    SMATRIX(context);
  2345. */
  2346.    #undef v
  2347. }
  2348.  
  2349. void GLTranslatef(GLcontext context, GLfloat x, GLfloat y, GLfloat z)
  2350. {
  2351. /*
  2352. layout:
  2353. 1 0 0 x
  2354. 0 1 0 y
  2355. 0 0 1 z
  2356. 0 0 0 1
  2357. */
  2358.  
  2359.    float vv[16];
  2360.    //LOG(3, glTranslatef, "%f %f %f", x,y,z);
  2361.  
  2362.    GLASSERT(context != NULL);
  2363.    context->InvRotValid = GL_FALSE;
  2364.    context->CombinedValid = GL_FALSE;
  2365.  
  2366.    #define v(x) (vv[OF_##x])
  2367.      v(11) = v(22) = v(33) = v(44) = 1.f;
  2368.      v(21) = v(31) = v(41) = v(12) = v(32) = v(42) = v(13) = v(23) = v(43) = 0.f;
  2369.  
  2370.    v(14) = (float)x; v(24) = (float)y; v(34) = (float)z;
  2371.  
  2372.    #undef v
  2373.  
  2374.    m_Mult(CMATRIX(context), vv, MGLMAT_TRANSLATION, OMATRIX(context));
  2375.    SMATRIX(context);
  2376.  
  2377. /*
  2378.    m_MatMultBTrans(CMATRIX(context), vv, OMATRIX(context));
  2379.    SMATRIX(context);
  2380. */
  2381. }
  2382.  
  2383. void GLMatrixInit(GLcontext context)
  2384. {
  2385.    context->ModelViewStackPointer = 0;
  2386.    context->ProjectionStackPointer = 0;
  2387.    context->ModelViewNr = 0;
  2388.    context->ProjectionNr = 0;
  2389.    context->CombinedValid = GL_FALSE;
  2390.    context->InvRotValid = GL_FALSE;
  2391.    GLMatrixMode(context, GL_PROJECTION);
  2392.    GLLoadIdentity(context);
  2393.    GLMatrixMode(context, GL_MODELVIEW);
  2394.    GLLoadIdentity(context);
  2395.  
  2396. }
  2397.  
  2398.